#include	<FW/System/Win32/Timer.h>
#include	<Windows.h>

namespace System
{
		Timer :: Timer	( void )
{
	Reset ( );
}

double	Timer :: GetTime	( void ) const
{
	if ( mStopped )
		return mStopTime;
	else
		return static_cast < double > ( GetSystemTime ( ) - mStartTime );
}

void	Timer :: Reset	( void )
{
	mStartTime = GetSystemTime ( );
}

void	Timer :: Start	( void )
{
	if ( mStopped )
	{
		mStartTime = mStopTime;
		mStopped = false;
	}
}

void	Timer :: Stop	( void )
{
	if ( !mStopped )
	{
		mStopTime = GetTime ( );
		mStopped = true;
	}
}

double	Timer :: GetSystemTime	( void )
{
	static LARGE_INTEGER	Frequency;
	static bool				UseHighPerformanceTimer = QueryPerformanceFrequency ( & Frequency );

	if ( UseHighPerformanceTimer )
	{
		LARGE_INTEGER CurrentTime;
		QueryPerformanceCounter(&CurrentTime);

		return static_cast < double > ( CurrentTime.QuadPart ) / Frequency.QuadPart;
	}
	else
		return GetTickCount() * 0.001;
}

void	Timer :: Sleep	( double Time )
{
	::Sleep( static_cast < DWORD > ( Time * 1000 ) );
}

}
